package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.workingProcedureCommand.AddWorkingProcedureCommand;
import com.woniu.command.workingProcedureCommand.DeleteWorkingProcedureCommand;
import com.woniu.command.workingProcedureCommand.UpdateWorkingProcedureCommand;
import com.woniu.inlet.vo.WorkingProcedureVo;
import com.woniu.outlet.po.WorkingProcedurePo;
import com.woniu.service.WorkingProcedureService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "工序管理")
@Slf4j
@RequestMapping("/workingProcedure/")
public class WorkingProcedureController {

    @Autowired
    private WorkingProcedureService workingProcedureService;

    @Autowired
    private SystemLogController systemLogController;

    @GetMapping("/workingProcedures")
    @ApiOperation("查询工序")
    public ResponseResult<List<WorkingProcedureVo>> findworkingProcedures() throws Exception{
        List<WorkingProcedurePo>  workingProcedurePos =  workingProcedureService.findWorkingProcedures();
        List<WorkingProcedureVo> workingProcedureVos = new ArrayList<>();
        //对象的深拷贝
        for (WorkingProcedurePo item : workingProcedurePos){
            WorkingProcedureVo workingProcedureVo = new WorkingProcedureVo();
            BeanUtils.copyProperties(item,workingProcedureVo);
            workingProcedureVos.add(workingProcedureVo);
        }
        systemLogController.addSystemLog("查询工序");
        return new ResponseResult<>(workingProcedureVos, "OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workingProcedures/id")
    @ApiOperation("根据id查询工序")
    public ResponseResult<WorkingProcedureVo> getWorkingProcedureById(Long id) throws Exception {
        WorkingProcedurePo workingProcedurePo = workingProcedureService.getWorkingProcedureById(id);

        //对象的深拷贝

        WorkingProcedureVo workingProcedureVo = new WorkingProcedureVo();
        BeanUtils.copyProperties(workingProcedurePo, workingProcedureVo);

        systemLogController.addSystemLog("根据id查询工序");
        return new ResponseResult<>(workingProcedureVo,"OK", ResultCode.SUCCESS.getCode());
    }



    @GetMapping("/workingProcedures/getSuperByCode")
    @ApiOperation("根据code查询上级工序")
    public ResponseResult<String> getSuperByCode(String code) {
        WorkingProcedurePo workingProcedurePo = null;
        try {
            workingProcedurePo = workingProcedureService.getWorkingProcedureByWorkingProcedureCode(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        systemLogController.addSystemLog("根据code查询上级工序");
        return new ResponseResult<>(workingProcedurePo.getSuperiorWorkingProcedureCode(),"OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workingProcedures/getByCode")
    @ApiOperation("根据code查询")
    public ResponseResult<WorkingProcedureVo> getByCode(String code) throws Exception {
        WorkingProcedurePo workingProcedurePo = null;

            workingProcedurePo = workingProcedureService.getByCode(code);
            if (StringUtils.isEmpty(workingProcedurePo)){
                return new ResponseResult<>(null, "没有参数", ResultCode.COMMON_FAIL.getCode());

            }
        //对象的深拷贝
        WorkingProcedureVo workingProcedureVo = new WorkingProcedureVo();
        BeanUtils.copyProperties(workingProcedurePo, workingProcedureVo);
        systemLogController.addSystemLog("根据code查询");
        return new ResponseResult<>(workingProcedureVo, "查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加工序")
    @ApiOperationSupport(ignoreParameters = {"id","workingProcedureCode","spare1","spare2","spare3"})
    public ResponseResult<String> addWorkingProcedure(WorkingProcedurePo workingProcedurePo){
        Command command = new AddWorkingProcedureCommand();
        command.execute(workingProcedurePo);
        systemLogController.addSystemLog("添加工序");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改工序")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateWorkingProcedure(WorkingProcedurePo workingProcedurePo){
        Command command = new UpdateWorkingProcedureCommand();
        command.execute(workingProcedurePo);
        systemLogController.addSystemLog("修改工序");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除工序")
    public ResponseResult<String> deleteWorkingProcedure(Long id){
        Command command = new DeleteWorkingProcedureCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除工序");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
